package vip.xiaonuo.issued.ack.handler;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.deviceset.service.DwDeviceSetService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.mapper.MemberDeviceMapper;
import vip.xiaonuo.facedoor.modular.memberdevice.service.MemberDeviceService;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
import vip.xiaonuo.facedoor.modular.orgdevice.entity.OrgDevice;
import vip.xiaonuo.facedoor.modular.orgdevice.service.OrgDeviceService;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.mapper.VisitorDeviceMapper;
import vip.xiaonuo.facedoor.modular.visitordevice.service.VisitorDeviceService;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.issued.ack.entity.BaseAck;
import vip.xiaonuo.facedoor.modular.devicebase.vo.lock.LockCell;
import vip.xiaonuo.issued.ack.entity.DeviceAck;
import vip.xiaonuo.issued.ack.entity.PersonAck;
import vip.xiaonuo.issued.bean.CMD;
import vip.xiaonuo.sys.modular.timer.param.SysTimersParam;
import vip.xiaonuo.sys.modular.timer.service.SysTimersService;

import javax.annotation.Resource;
import java.util.*;

import static vip.xiaonuo.issued.bean.CMD.FAIL;

@Slf4j
@Service
public class FacedoorMessageHandler implements MessageHandler {


  @Resource
  private DeviceLogService deviceLogService;

  @Resource
  private DeviceBaseService deviceBaseService;

  @Autowired
  private OrgDeviceService orgDeviceService;

  @Autowired
  private MemberDeviceService memberDeviceService;

  @Autowired
  private SysTimersService sysTimersService;

  @Autowired
  private VisitorDeviceService visitorDeviceService;

  @Resource
  private MemberDeviceMapper memberDeviceMapper;

  @Resource
  private MemberUserService memberUserService;

  @Resource
  private VisitorService visitorService;

  @Resource
  private VisitorDeviceMapper visitorDeviceMapper;


  @Resource
  private DwDeviceSetService deviceSetService;


  @Override
  public void handleMessage(Message<?> message) throws MessagingException {
    try {
      log.info("收到的完整消息为--->{}", message);
      log.info("----------------------");
      log.info("message:" + message.getPayload());
      log.info("Id:" + message.getHeaders().getId());
      log.info("receivedQos:" + message.getHeaders().get(MqttHeaders.RECEIVED_QOS));
      String topic = (String) message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC);
      log.info("topic:" + topic);
      log.info("----------------------");
      JSONObject jsonObject = JSON.parseObject(message.getPayload().toString());
      int cmd = Integer.parseInt(jsonObject.get("cmd").toString());
      switch (cmd) {
        case CMD.REBOOT_DEVICE:
          rebootDevice(message);
          break;
        case CMD.DEVICE_STATE:
          deviceState(message);
          break;
        case CMD.UPLOAD_PARAM:
          uploadParam(message);
          break;
        case CMD.DELETE_ALL_PERSON:
          deleteAllPerson(message);
          break;
        case CMD.DELETE_PERSON:
          deletePerson(message);
          break;
        case CMD.ADD_PERSON:
          addPerson(message);
          break;
        case CMD.UPDATE_PERSON:
          updatePerson(message);
          break;
      }
    } catch (Exception e) {
      log.error("消费MQTT消息发生错误【{}】", ExceptionUtils.getMessage(e));
    }
  }

  private void updatePerson(Message<?> message) {
    PersonAck personAck = JSON.parseObject(message.getPayload().toString(), PersonAck.class);
    deviceLogService.updateDeviceLog(personAck.getSerial_number(), message.getPayload().toString(), personAck.getCode());
    if (ObjectUtil.equal(personAck.getMemberType(), 1)) {
      MemberDevice memberDevice = memberDeviceService.getById(personAck.getMemberOrVisitorDeviceId());
      if (memberDevice == null) {
        log.error("人员设备关联关系已经不存在！ 跳过执行 关联id【{}】", personAck.getMemberOrVisitorDeviceId());
        return;
      }
      MemberUser memberUser = memberUserService.getById(memberDevice.getMemberId());
      if (memberUser == null) {
        log.error("人员关联关系已经不存在！ 跳过执行 人员id【{}】", memberDevice.getMemberId());
        memberDevice.setFailReason("修改人员发生错误：" + JSON.toJSONString(personAck));
        memberDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog("", "", JSON.toJSONString(personAck), FAIL, CMD.UPDATE);
        return;
      }
      DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
      if (deviceBase == null) {
        log.error("设备已经不存在！ 跳过执行 设备id【{}】", memberDevice.getDeviceId());
        memberDevice.setFailReason("修改人员发生错误：" + JSON.toJSONString(personAck));
        memberDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), "", JSON.toJSONString(personAck), FAIL, CMD.UPDATE);
        return;
      }
      if (StringUtils.isNotBlank(personAck.getCode()) && ("0000".equals(personAck.getCode()))) {
        memberDevice.setFailReason(null);
        memberDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
        memberDeviceMapper.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), JSON.toJSONString(personAck), CMD.SUCCESS, CMD.UPDATE);
      } else if (StringUtils.isNotBlank(personAck.getCode()) && ("0201".equals(personAck.getCode()))) {
        memberDevice.setFailReason("人员不存在重新添加人员！");
        memberDevice.setStatus(MemberDeviceConstant.ADD);
        memberDeviceMapper.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), JSON.toJSONString(personAck), FAIL, CMD.UPDATE);
      }else {
        memberDevice.setFailReason("修改人员发生错误：" + JSON.toJSONString(personAck));
        memberDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), JSON.toJSONString(personAck), FAIL, CMD.UPDATE);
      }
    } else if (ObjectUtil.equal(personAck.getMemberType(), 2)) {
      VisitorDevice visitorDevice = visitorDeviceService.getById(personAck.getMemberOrVisitorDeviceId());
      if (visitorDevice == null) {
        log.error("访客设备关联关系已经不存在！ 跳过执行 关联id【{}】", personAck.getMemberOrVisitorDeviceId());
        return;
      }
      Visitor visitor = visitorService.getById(visitorDevice.getVisitorId());
      if (visitor == null) {
        log.error("访客关联关系已经不存在！ 跳过执行 人员id【{}】", visitorDevice.getVisitorId());
        visitorDevice.setFailReason("修改访客发生错误：访客关联关系已经不存在!" + JSON.toJSONString(personAck));
        visitorDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        visitorDeviceMapper.updateById(visitorDevice);
        deviceLogService.addMemberLog("", "", JSON.toJSONString(personAck), FAIL, CMD.UPDATE);
        return;
      }
      DeviceBase deviceBase = deviceBaseService.getById(visitorDevice.getDeviceId());
      if (deviceBase == null) {
        log.error("设备已经不存在！ 跳过执行 设备id【{}】", visitorDevice.getDeviceId());
        visitorDevice.setFailReason("修改访客发生错误：设备已经不存在!" + JSON.toJSONString(personAck));
        visitorDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        visitorDeviceMapper.updateById(visitorDevice);
        deviceLogService.addMemberLog(visitor.getName(), "", JSON.toJSONString(personAck), FAIL, CMD.UPDATE);
        return;
      }
      if (StringUtils.isNotBlank(personAck.getCode()) && ("0000".equals(personAck.getCode()))) {
        visitorDevice.setFailReason(null);
        visitorDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
        visitorDeviceMapper.updateById(visitorDevice);
        deviceLogService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(personAck), CMD.SUCCESS, CMD.UPDATE);
      } else {
        visitorDevice.setFailReason("修改访客发生错误：" + JSON.toJSONString(personAck));
        visitorDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        visitorDeviceMapper.updateById(visitorDevice);
        deviceLogService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(personAck), FAIL, CMD.UPDATE);
      }
    }
  }

  private void addPerson(Message<?> message) {
    PersonAck addPersonAck = JSON.parseObject(message.getPayload().toString(), PersonAck.class);
    deviceLogService.updateDeviceLog(addPersonAck.getSerial_number(), message.getPayload().toString(), addPersonAck.getCode());
    if (ObjectUtil.equal(addPersonAck.getMemberType(), 1)) {
      MemberDevice memberDevice = memberDeviceService.getById(addPersonAck.getMemberOrVisitorDeviceId());
      if (memberDevice == null) {
        log.error("人员设备关联关系已经不存在！ 跳过执行 关联id【{}】", addPersonAck.getMemberOrVisitorDeviceId());
        return;
      }
      MemberUser memberUser = memberUserService.getById(memberDevice.getMemberId());
      if (memberUser == null) {
        log.error("人员关联关系已经不存在！ 跳过执行 人员id【{}】", memberDevice.getMemberId());
        log.error("设备已经不存在！ 跳过执行 设备id【{}】", memberDevice.getDeviceId());
        memberDevice.setFailReason("新增人员发生错误：人员关联关系已经不存在" + JSON.toJSONString(addPersonAck));
        memberDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog("", "", JSON.toJSONString(addPersonAck), FAIL, CMD.ADD);
        return;
      }
      DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
      if (deviceBase == null) {
        log.error("设备已经不存在！ 跳过执行 设备id【{}】", memberDevice.getDeviceId());
        memberDevice.setFailReason("新增人员发生错误：设备已经不存在" + JSON.toJSONString(addPersonAck));
        memberDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), "", JSON.toJSONString(addPersonAck), FAIL, CMD.ADD);
        return;
      }
      if (StringUtils.isNotBlank(addPersonAck.getCode()) && ("0000".equals(addPersonAck.getCode()))) {
        memberDevice.setFailReason(null);
        memberDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
        memberDeviceMapper.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), JSON.toJSONString(addPersonAck), CMD.SUCCESS, CMD.ADD);
      } else {
        memberDevice.setFailReason("新增人员发生错误：" + JSON.toJSONString(addPersonAck));
        memberDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), JSON.toJSONString(addPersonAck), FAIL, CMD.ADD);
      }
    } else if (ObjectUtil.equal(addPersonAck.getMemberType(), 2)) {
      VisitorDevice visitorDevice = visitorDeviceService.getById(addPersonAck.getMemberOrVisitorDeviceId());
      if (visitorDevice == null) {
        log.error("访客设备关联关系已经不存在！ 跳过执行 关联id【{}】", addPersonAck.getMemberOrVisitorDeviceId());
        return;
      }
      Visitor visitor = visitorService.getById(visitorDevice.getVisitorId());
      if (visitor == null) {
        log.error("访客关联关系已经不存在！ 跳过执行 人员id【{}】", visitorDevice.getVisitorId());
        visitorDevice.setFailReason("访客关联关系已经不存在：" + JSON.toJSONString(addPersonAck));
        visitorDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        visitorDeviceMapper.updateById(visitorDevice);
        deviceLogService.addMemberLog("", "", JSON.toJSONString(addPersonAck), FAIL, CMD.ADD);
        return;
      }
      DeviceBase deviceBase = deviceBaseService.getById(visitorDevice.getDeviceId());
      if (deviceBase == null) {
        log.error("设备已经不存在！ 跳过执行 设备id【{}】", visitorDevice.getDeviceId());
        visitorDevice.setFailReason("设备已经不存在：" + JSON.toJSONString(addPersonAck));
        visitorDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        visitorDeviceMapper.updateById(visitorDevice);
        deviceLogService.addMemberLog(visitor.getName(), "", JSON.toJSONString(addPersonAck), FAIL, CMD.ADD);
        return;
      }
      if (StringUtils.isNotBlank(addPersonAck.getCode()) && ("0000".equals(addPersonAck.getCode()))) {
        visitorDevice.setFailReason(null);
        visitorDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
        visitorDeviceMapper.updateById(visitorDevice);
        deviceLogService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(addPersonAck), CMD.SUCCESS, CMD.ADD);
      } else {
        visitorDevice.setFailReason("新增人员发生错误：" + JSON.toJSONString(addPersonAck));
        visitorDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        visitorDeviceMapper.updateById(visitorDevice);
        deviceLogService.addMemberLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(addPersonAck), FAIL, CMD.ADD);
      }
    }

  }

  private void deletePerson(Message<?> message) {
    PersonAck deletePersonAck = JSON.parseObject(message.getPayload().toString(), PersonAck.class);
    deviceLogService.updateDeviceLog(deletePersonAck.getSerial_number(), message.getPayload().toString(), deletePersonAck.getCode());
    if (ObjectUtil.equal(deletePersonAck.getMemberType(), 1)) {
      MemberDevice memberDevice = memberDeviceService.getById(deletePersonAck.getMemberOrVisitorDeviceId());
      if (memberDevice == null) {
        log.error("人员设备关联关系已经不存在！ 跳过执行 关联id【{}】", deletePersonAck.getMemberOrVisitorDeviceId());
        return;
      }
      MemberUser memberUser = memberUserService.getById(memberDevice.getMemberId());
      if (memberUser == null) {
        log.error("人员关联关系已经不存在！ 跳过执行 人员id【{}】", memberDevice.getMemberId());
        memberDevice.setFailReason("人员关联关系已经不存在！");
        memberDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog("", "", message.getPayload().toString(), FAIL, CMD.DELETE);
        return;
      }
      DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
      if (deviceBase == null) {
        log.error("设备已经不存在！ 跳过执行 设备id【{}】", memberDevice.getDeviceId());
        memberDevice.setFailReason("设备已经不存在！");
        memberDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), "", message.getPayload().toString(), FAIL, CMD.DELETE);
        return;
      }
      // 删除用户成功
      if (StringUtils.isNotBlank(deletePersonAck.getCode()) && ("0000".equals(deletePersonAck.getCode()) || "0201".equals(deletePersonAck.getCode()))) {
        memberDeviceService.removeById(memberDevice.getId());
        long count = memberDeviceMapper.selectCount(new LambdaQueryWrapper<MemberDevice>().eq(MemberDevice::getMemberId, memberDevice.getMemberId()));
        if (count == 0 && ObjectUtil.equal(memberUser.getDelFlag(), "1")) {
          memberUserService.removeById(memberDevice.getMemberId());
        }
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), message.getPayload().toString(), CMD.SUCCESS, CMD.DELETE);
        // 删除人员失败 -- 其他原因
      } else {
        memberDevice.setFailReason(JSON.toJSONString(message.getPayload().toString()));
        memberDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), message.getPayload().toString(), FAIL, CMD.DELETE);
      }
    } else if (ObjectUtil.equal(deletePersonAck.getMemberType(), 2)) {
      VisitorDevice visitorDevice = visitorDeviceService.getById(deletePersonAck.getMemberOrVisitorDeviceId());
      if (visitorDevice == null) {
        log.error("访客设备关联关系已经不存在！ 跳过执行 关联id【{}】", deletePersonAck.getMemberOrVisitorDeviceId());

        return;
      }
      Visitor visitor = visitorService.getById(visitorDevice.getVisitorId());
      if (visitor == null) {
        visitorDevice.setFailReason("访客关联关系已经不存在");
        visitorDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        visitorDeviceService.updateById(visitorDevice);
        deviceLogService.addVisitorLog("", "", message.getPayload().toString(), FAIL, CMD.DELETE);
        log.error("访客关联关系已经不存在！ 跳过执行 人员id【{}】", visitorDevice.getVisitorId());
        return;
      }
      DeviceBase deviceBase = deviceBaseService.getById(visitorDevice.getDeviceId());
      if (deviceBase == null) {
        log.error("设备已经不存在！ 跳过执行 设备id【{}】", visitorDevice.getDeviceId());
        visitorDevice.setFailReason("访客关联关系已经不存在");
        visitorDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        visitorDeviceService.updateById(visitorDevice);
        deviceLogService.addVisitorLog(visitor.getName(), "", message.getPayload().toString(), FAIL, CMD.DELETE);
        log.error("访客关联关系已经不存在！ 跳过执行 人员id【{}】", visitorDevice.getVisitorId());
        return;
      }
      // 删除用户成功
      if (StringUtils.isNotBlank(deletePersonAck.getCode()) && ("0000".equals(deletePersonAck.getCode()) || "0201".equals(deletePersonAck.getCode()))) {
        visitorDeviceService.removeById(visitorDevice.getId());
        long count = visitorDeviceMapper.selectCount(new LambdaQueryWrapper<VisitorDevice>().eq(VisitorDevice::getVisitorId, visitorDevice.getVisitorId()));
        if (count == 0 && ObjectUtil.equal(visitor.getDelFlag(), "1")) {
          visitorService.removeById(visitor.getId());
        }
        deviceLogService.addVisitorLog(visitor.getName(), deviceBase.getName(), message.getPayload().toString(), CMD.SUCCESS, CMD.DELETE);
        // 删除人员失败 -- 其他原因
      } else {
        visitorDevice.setFailReason(JSON.toJSONString(message.getPayload().toString()));
        visitorDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        visitorDeviceService.updateById(visitorDevice);
        deviceLogService.addVisitorLog(visitor.getName(), deviceBase.getName(), message.getPayload().toString(), FAIL, CMD.DELETE);
      }

    }

  }


  private void deleteAllPerson(Message<?> message) {
    BaseAck baseAck = JSON.parseObject(message.getPayload().toString(), BaseAck.class);
    deviceLogService.updateDeviceLog(baseAck.getSerial_number(), message.getPayload().toString(), baseAck.getCode());
    DeviceBase deviceBase = deviceBaseService.getById(baseAck.getDeviceId());
    if (deviceBase == null) {
      log.error("设备已经不存在！ 跳过执行 设备id【{}】", baseAck.getDeviceId());
      return;
    }
    deviceBaseService.removeById(baseAck.getDeviceId());
    orgDeviceService.remove(new LambdaQueryWrapper<OrgDevice>().eq(OrgDevice::getDeviceId, baseAck.getDeviceId()));
    memberDeviceService.remove(new LambdaQueryWrapper<MemberDevice>().eq(MemberDevice::getDeviceId, baseAck.getDeviceId()));
    visitorDeviceService.remove(new LambdaQueryWrapper<VisitorDevice>().eq(VisitorDevice::getDeviceId, baseAck.getDeviceId()));
    // 定时任务删除
    SysTimersParam sysTimersParam = new SysTimersParam();
    sysTimersParam.setId(baseAck.getDeviceId());
    sysTimersService.delete(sysTimersParam);
  }

  private void uploadParam(Message<?> message) {
    BaseAck baseAck = JSON.parseObject(message.getPayload().toString(), BaseAck.class);
    deviceLogService.updateDeviceLog(baseAck.getSerial_number(), message.getPayload().toString(), baseAck.getCode());
  }

  private void deviceState(Message<?> message) {
    DeviceAck baseAck = JSON.parseObject(message.getPayload().toString(), DeviceAck.class);
    DeviceBase deviceBase = deviceBaseService.getById(baseAck.getDeviceId());
    if (deviceBase == null) {
      log.error("设备已经不存在！ 跳过执行 设备id【{}】", baseAck.getDeviceId());
      return;
    }
    log.info("收到设备心跳信息【{}】", deviceBase.getName());
    deviceBase.setDeviceState("1");
    deviceBase.setCounter(0);
    deviceBase.setMemberCount(baseAck.getData().getMemberCount());
    deviceBase.setLastOnlineTime(new Date());
    // 处理存储柜相关数据
    if(ObjectUtil.equal(deviceBase.getDeviceType(),"5")){
      deviceBase.setRowNum(baseAck.getData().getHorizontNum());
      deviceBase.setColNum(baseAck.getData().getVerticalNum());
      deviceBase.setMaxUseNum(baseAck.getData().getMaxUsageNum());
      deviceBase.setAdminPwd(baseAck.getData().getAdminPwd());
      deviceBase.setLockCellState(this.convert(deviceBase,baseAck.getData().getCabinetStatus()));
    }
    deviceBaseService.updateById(deviceBase);
  }

  private String convert(DeviceBase deviceBase, String data) {
    if (ObjectUtil.isEmpty(data)) {
      return "未回传格口状态数据";
    }
    // 获取行数和列数
    int rowNum = 0;
    int colNum = 0;

    if (deviceBase != null) {
      rowNum = deviceBase.getRowNum() == null ? 0 : deviceBase.getRowNum();
      colNum = deviceBase.getColNum() == null ? 0 : deviceBase.getColNum();
    }
    if (data.length() != rowNum * colNum) {
      return "数据长度与格口数量不一致,数据：" + data + ",长度：" + data.length() + ",格口数量：" + rowNum * colNum;
    }

    List<LockCell> cellStateList = new LinkedList<>();
    for (int x = 1; x <= colNum; x++) {
      for (int y = 1; y <= rowNum; y++) {
        int index = (x - 1) * rowNum + y - 1;
        LockCell cellState = new LockCell(x, y, Integer.parseInt(data.substring(index, index + 1)));
        cellStateList.add(cellState);
      }
    }
    return JSON.toJSONString(cellStateList);
  }

  private void rebootDevice(Message<?> message) {
    BaseAck baseAck = JSON.parseObject(message.getPayload().toString(), BaseAck.class);
    deviceLogService.updateDeviceLog(baseAck.getSerial_number(), message.getPayload().toString(), baseAck.getCode());
  }

}
